import winterwell.jtwitter.Twitter
class AdministradorController {
    def index = { }
    def categorias = {}
    def eventos = {}
    def usuarios = {}

    def listcateg = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ categoriaInstanceList: Categoria.list( params ), categoriaInstanceTotal: Categoria.count() ]
    }

    def showcateg = {
        def categoriaInstance = Categoria.get( params.id )

        if(!categoriaInstance) {
            flash.message = "Categoria not found with id ${params.id}"
            redirect(action:listcateg)
        }
        else { return [ categoriaInstance : categoriaInstance ] }
    }
    
    def createcateg = {
        def categoriaInstance = new Categoria()
        categoriaInstance.properties = params
        return ['categoriaInstance':categoriaInstance]
    }

    def savecateg = {
        def categoriaInstance = new Categoria(params)
        if(!categoriaInstance.hasErrors() && categoriaInstance.save()) {
            //flash.message = "Categoria ${categoriaInstance.id} created"
            
            redirect(action:showcateg, id:categoriaInstance.id)
        }
        else {
            render(view:'createcateg',model:[categoriaInstance:categoriaInstance])
        }
    }

    def deletecateg = {
        def categoriaInstance = Categoria.get( params.id )
        if(categoriaInstance) {
            try {
                categoriaInstance.delete(flush:true)
                redirect(action:listcateg)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Categoria ${params.id} could not be deleted"
                redirect(action:showcateg,id:params.id)
            }
        }
        else {
            flash.message = "Categoria not found with id ${params.id}"
            redirect(action:listcateg)
        }
    }

    def editcateg = {
        def categoriaInstance = Categoria.get( params.id )

        if(!categoriaInstance) {
            flash.message = "Categoria not found with id ${params.id}"
            redirect(action:listcateg)
        }
        else {
            return [ categoriaInstance : categoriaInstance ]
        }
    }

    def updatecateg = {
        def categoriaInstance = Categoria.get( params.id )
        if(categoriaInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(categoriaInstance.version > version) {

                    categoriaInstance.errors.rejectValue("version", "categoria.optimistic.locking.failure", "Another user has updated this Categoria while you were editing.")
                    render(view:'editcateg',model:[categoriaInstance:categoriaInstance])
                    return
                }
            }
            categoriaInstance.properties = params
            if(!categoriaInstance.hasErrors() && categoriaInstance.save()) {
                redirect(action:showcateg,id:categoriaInstance.id)
            }
            else {
                render(view:'editcateg',model:[categoriaInstance:categoriaInstance])
            }
        }
        else {
            flash.message = "Categoria not found with id ${params.id}"
            redirect(action:listcateg)
        }
    }

    // Mantenimiento eventos

       def createev = {
        def eventoInstance = new Evento()
        eventoInstance.properties = params
        return ['eventoInstance':eventoInstance]
    }

    def saveev = {
        def eventoInstance = new Evento(params)
        if(!eventoInstance.hasErrors() && eventoInstance.save()) {
            //flash.message = "Evento ${eventoInstance.id} created"
            redirect(action:createep, id:eventoInstance.id)
        }
        else {
            render(view:'createev',model:[eventoInstance:eventoInstance]) 
        }
    }

    def listev = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ eventoInstanceList: Evento.list( params ), eventoInstanceTotal: Evento.count() ]
    }

    def showev = {
        def eventoInstance = Evento.get( params.id )

        if(!eventoInstance) {
            flash.message = "Evento not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ eventoInstance : eventoInstance ] }

    }

    def createep = {
        def eventoInstance = Evento.get( params.id )
        def eventoParticipanteInstance = new EventoParticipante()
        eventoParticipanteInstance.properties = params
        return ['eventoParticipanteInstance':eventoParticipanteInstance,  eventoInstance : eventoInstance ]
    }

    def saveep = {
        def eventoParticipanteInstance = new EventoParticipante(params)
        if(!eventoParticipanteInstance.hasErrors() && eventoParticipanteInstance.save()) {
            //flash.message = "EventoParticipante ${eventoParticipanteInstance.id} created"
            redirect(action:showev, id:eventoParticipanteInstance.event.id)
        }
        else {
            render(view:'createep',model:[eventoParticipanteInstance:eventoParticipanteInstance])
        }
    }

     def showev2 = {
        def eventoInstance = Evento.get( params.id )

        if(!eventoInstance) {
            flash.message = "Evento not found with id ${params.id}"
            redirect(action:listev)
        }
        else { return [ eventoInstance : eventoInstance ] }
    }

        def showevp = {
        //def eventoParticipanteInstance = EventoParticipante.get( params.id )
        def eventoInstance = Evento.get( params.id )
        if(!eventoInstance) {
            flash.message = "EventoParticipante not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            def participanteInstance = EventoParticipante.findAllByEvent(eventoInstance)
            //def participanteInstance = Participante.findById(participante)
            return [ eventoInstance : eventoInstance, participanteInstance:participanteInstance ]
            //return [participanteInstance:participanteInstance]
        }
    }

    def updateevp = {
        def eventoParticipanteInstance = EventoParticipante.get( params.ganador )
        eventoParticipanteInstance.ganador = 'S'
        eventoParticipanteInstance.save()

        def gana = eventoParticipanteInstance.part.nombre

        def even = eventoParticipanteInstance.event.nombre

        def twitter = new Twitter("casaapuestaeel","casaapuesta")
        twitter.setStatus("El ganador del evento ${even} fue ${gana}. Felicidades a los ganadores!")

        def apu = Apuesta.findByEvent( eventoParticipanteInstance )

        apu.each()
        {
            def notifInstance = new Notificacion();

            notifInstance.user = apu.mpagousu.user

            notifInstance.descripcion = "usted ${apu.mpagousu.user.nombre} ha ganado en la apuesta realizada en el evento ${apu.event.event.nombre}"

            notifInstance.save();
            //print apu.mpagousu.user.nombre;
        };

        redirect(action: eventos)

    }

    def createpart = {
        def participanteInstance = new Participante()
        participanteInstance.properties = params
        return ['participanteInstance':participanteInstance]
    }

    def savepart = {
        def participanteInstance = new Participante(params)
        if(!participanteInstance.hasErrors() && participanteInstance.save()) {
            //flash.message = "Participante ${participanteInstance.id} created"
            redirect(action:eventos, id:participanteInstance.id)
        }
        else {
            render(view:'create',model:[participanteInstance:participanteInstance])
        }
    }
    
}
